using Autofac;

using Cookbook.Services.Core.Persistency.NHibernate;
using Cookbook.Services.Core.Persistency.NHibernate.ExceptionHandling;

namespace Cookbook.Services.Core.Persistency
{
    public class CookbookUnitOfWorkModule : NHibernateUnitOfWorkModule<CookbookUnitOfWork>
    {
        public CookbookUnitOfWorkModule(string connectionStringName) : base(connectionStringName)
        {

        }

        /// <summary>
        /// Can be overriden to register any additional exception policies that need to be registered before
        /// the <see cref="NHibernateExceptionInterceptor"/> is registered.
        /// </summary>
        protected override void RegisterExceptionPolicies(ContainerBuilder builder)
        {
            builder.RegisterType<CookbookExceptionPolicy>().As<INHibernateExceptionPolicy>();
        }

        /// <summary>
        /// Must be implemented to register a factory for resolving instances of <see cref="TUnitOfWork"/>.
        /// </summary>
        protected override void RegisterFactory(ContainerBuilder builder)
        {
            builder.RegisterType<CookbookUnitOfWorkFactory>();
            builder
                .Register(c => c.Resolve<CookbookUnitOfWorkFactory>().Create())
                .ExternallyOwned();
        }
    }
}